const domainName = getApp().globalData.domainName
const parameter = getApp().globalData.parameter

// pages/chap_doti_rec/chap_doti_rec.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page_no:0,
    questions_for_check:[],
  },

  // 同意通过
  agree(e){
    let index = e.currentTarget.id
    let ti_id = this.data.questions_for_check[index].ti_id
    wx.showModal({
      content: '确定要通过该题吗？',
      title: '提示',
      success: res => {
        if(res.confirm){
          // console.log('确定通过')
          this.setData({
            questions_for_check:[],
            page_no:0,
          })
          wx.request({
            url: domainName + '/set_ti_status2',
            data: {
              ti_id,
            },...parameter,
            success: res => {
              console.log('res',res)
              if(res.data.update_res){
                wx.showToast({
                  title: '操作完成！',
                  duration: 1500,
                  icon: 'success',
                })
                this.onLoad(this.options)
              }
            },
          })
        }
      },
    })
  },


  // 拒绝通过
  reject(e){
    let index = e.currentTarget.id
    let ti_id = this.data.questions_for_check[index].ti_id
    wx.showModal({
      showCancel: true,
      title: '该题不通过！',
      success: res => {
        if(res.confirm){
          wx.request({
            url: domainName + '/reject_ti',
            data: {
              ti_id,
            },...parameter,
            success: res => {
              if(res.data.update_res){
                wx.showToast({
                  title: '操作完成!',
                  duration: 1500,
                  icon: 'success',
                })
                this.onLoad(this.options)
              }
            },
          })
          // console.log('拒绝通过', content)
        }
      },
    })
  },


  // 加载习题列表
  load_questions_for_check(page_no,load_more){
    wx.showLoading({
      title: '网络通讯中',
      mask: false,
    })
    wx.request({
      url: domainName + '/questions_for_check',
      data: {page_no},...parameter,
      success: res => {
        console.log('res',res.data)
        wx.hideLoading()
        var questions_for_check = res.data.lis
        if(load_more){
          if(questions_for_check.length>0){
            this.setData({
              questions_for_check: this.data.questions_for_check.concat(questions_for_check),
              page_no:this.data.page_no + 1,
            })
          }else{
            this.setData({
              no_more:true
            })
          }
        }else{
          this.setData({
            questions_for_check:questions_for_check,
            page_no:1
          })
          if(questions_for_check.length==0){
            this.setData({
              no_more:true
            })
          }
        }
      },
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.load_questions_for_check(0,false)
  },

  onPullDownRefresh(){
    this.onLoad(this.options)
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.load_questions_for_check(this.data.page_no, true)
  },

})